package org.mappland.compltres.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mappland.compltres.common.CommonResult;
import org.mappland.compltres.common.StatusCode;
import org.mappland.compltres.domain.Order;
import org.mappland.compltres.domain.User;
import org.mappland.compltres.service.MachineService;
import org.mappland.compltres.service.OrderService;
import org.mappland.compltres.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminOrderController")
@RequestMapping("/admin")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private MachineService machineService;
    @Resource
    private UserService userService;

    // 查看订单
    @GetMapping("/listOrders")
    public CommonResult<List<Order>> listOrders(@RequestParam("orderFlags") List<Integer> flags) {
        CommonResult<List<Order>> result = new CommonResult<>();
        QueryWrapper<Order> wrapper = new QueryWrapper<>();

        wrapper.in("flag", flags);
        List<Order> orders = orderService.list(wrapper);

        result.setCode(StatusCode.COMMON_SUCCESS.getCode());
        result.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        result.setData(orders);

        return result;
    }
    // 正常租赁处理
    @PostMapping("/handle")
    public CommonResult<String> handle(@RequestParam("orderId") Integer orderId) {
        CommonResult<String> return_result = new CommonResult<>();
        Order order = orderService.getById(orderId);
        // 设置订单状态
        order.setFlag(1);
        boolean result = orderService.updateById(order);

        if (result) {
            machineService.bookMachine(order.getMachineId());
            User user = userService.getById(order.getUserId());

            // 增加用户积分
            int point = (int) (user.getPoints() + order.getRealPrice());
            user.setPoints(point);
            userService.updateById(user);

            return_result.setCode(StatusCode.COMMON_SUCCESS.getCode());
            return_result.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            return_result.setData("预定机器成功!");
        } else {
            return_result.setCode(StatusCode.COMMON_FAIL.getCode());
            return_result.setMessage(StatusCode.COMMON_FAIL.getMessage());
            return_result.setData("预定机器成功!");
        }
        return return_result;
    }
        // 订单退订
    @PostMapping("/unsubscribe")
    public CommonResult<String> unsubscribe(@RequestParam("orderId") Integer orderId) {
        CommonResult<String> return_result = new CommonResult<>();

        Order order = orderService.getById(orderId);
        // 设置订单状态为取消
        order.setFlag(2);

        boolean result = orderService.updateById(order);

        if (result) {
            return_result.setCode(StatusCode.COMMON_SUCCESS.getCode());
            return_result.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            return_result.setData("退定成功!");
        } else {
            return_result.setCode(StatusCode.COMMON_FAIL.getCode());
            return_result.setMessage(StatusCode.COMMON_FAIL.getMessage());
            return_result.setData("退定失败!");
        }
        return return_result;
    }
}
